<?php
class Zimage {
	private $width = "640";
	private $heidth = "480";
	private $image = null;
	private $image_type = null;
	
	private function load($filename) {
	   $image_info = getimagesize($filename);
       $this->image_type = $image_info[2];
       if( $this->image_type == IMAGETYPE_JPEG ) {
           $this->image = imagecreatefromjpeg($filename);
       } elseif( $this->image_type == IMAGETYPE_GIF ) {
           $this->image = imagecreatefromgif($filename);
       } elseif( $this->image_type == IMAGETYPE_PNG ) {
           $this->image = imagecreatefrompng($filename);
       }
	}
	
	/**
	 * Enter description here ...
	 * @param int $t_width the target width
	 * @param int $t_height the target height
	 */
	private function scale($t_width, $t_height) {
		if ($t_width == NULL || $t_width == 0) {
			$t_width = $this->width;
		} 
		
		if ($t_height == NULL || $t_height == 0) {
			$t_height = $this->heidth;
		}
		
		$cur_width = imagesx($this->image);
		$cur_height = imagesy($this->image);
		
		if ($cur_width <= $t_width && $cur_height <= $t_height) {
			return $this->image;
		}
		
		/*echo "<br/>Cur_width = ".$cur_width;
		echo "<br/>Cur_height = ".$cur_height;
		*/
		$x_ratio = $t_width / $cur_width; 
		$y_ratio = $t_height / $cur_height;
		
		$ratio = $x_ratio >= $y_ratio ? $y_ratio : $x_ratio;
		
		$new_width = $ratio * $cur_width;
		$new_height = $ratio * $cur_height;
		
		/*
		echo "<br/>new_width = ".$new_width;
		echo "<br/>new_height = ".$new_width;
		*/
		$new_image = imagecreatetruecolor($new_width, $new_height);
		imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $new_width, $new_height, $cur_width, $cur_height);
		return $new_image;
	}
	
    private function save($filename, $new_image, $compression=80, $permissions=null) {
 
      if( $this->image_type == IMAGETYPE_JPEG ) {
         imagejpeg($new_image,$filename,$compression);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
 
         imagegif($new_image,$filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
 
         imagepng($new_image,$filename);
      }
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   
   private function watermark(&$new_image, $WaterMarkText) {
   	    $width = imagesx($new_image);
		$height = imagesy($new_image);
		$black = imagecolorallocate($new_image, 74, 145, 198);
        $font = FCPATH."/font/calibrib.ttf";
        $font_size = 10.5;
        
        $newWidth = imagesx($new_image);
        $newHeight = imagesy($new_image);
        $background = imagecolorallocatealpha($new_image,255,255,255,127);
        imagefilledrectangle($new_image,0,10,$newWidth,$newHeight,$background);          
        imagettftext($new_image, $font_size, 0, 10, $height -25, $black, $font, $WaterMarkText);
   }
   
   function resize($filename_origin, $filename_target, $water_mark=true, $t_width=null, $t_height=null) {
		$this->load($filename_origin);
		$new_image = $this->scale($t_width, $t_height);
		if ($water_mark) {
			$Z_USER = Zsession::getUserFromSession();
			$this->watermark($new_image,"gozuka.com \npost by ".$Z_USER["user_login"]);
		}
		$this->save($filename_target, $new_image);
	}
}
?>